package Server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.rmi.ssl.SslRMIServerSocketFactory;

import General.ShipInterface;


import java.rmi.MarshalledObject;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.activation.Activatable;
import java.rmi.activation.ActivationDesc;
import java.rmi.activation.ActivationGroup;
import java.rmi.activation.ActivationGroupDesc;
import java.rmi.activation.ActivationGroupID;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RemoteServer;
import java.rmi.server.UnicastRemoteObject;
import java.util.Properties;
import java.util.Random;

public class Main {
	
	public static void main(String[] args) throws RemoteException {		
		try {			
			//System.setProperty("java.rmi.server.codebase", "http://localhost:8080/");
	        //System.setProperty("java.security.policy", "Policies/server.policy");
	        //System.setProperty("javax.net.ssl.trustStore", "Policies/ServerTrustStore");
	        //System.setProperty("javax.net.ssl.keyStore", "Policies/ServerKeyStore");
	        //System.setProperty("javax.net.ssl.keyStorePassword", "nieldeckx");
	        
			if(System.getSecurityManager() == null) {
				System.setSecurityManager(new RMISecurityManager());
			}
			
	        String address = "localhost";
	        int port = 1099;
	        
	        if (args.length > 0)
	        	address = args[0];
	        if (args.length > 1)
	        	port =  Integer.valueOf(args[1]);
	        
			Registry registry = LocateRegistry.getRegistry(address, port);
			Registry registryImplementation = (Registry)registry.lookup("RegistryImplementation");
						
			System.out.println("[SERVER] Loading...");
			
			// Create name that is not yet used in the registry.
			String list[] = registryImplementation.list();
			boolean registered = false;
			String name = "";
			while (!registered) {
				Random randomGenerator = new Random();
				name = "Server_" + (randomGenerator.nextInt(1000) + 5);
				registered = true;
				for (String string : list) {
					if (string.contentEquals(name)) {
						registered = false;
					}
				}
			}
			ShipImplementation shipInterface = new ShipImplementation(name, 10, 30, 50);
			
			/*
			Properties properties = new Properties();
			properties.put("java.security.policy", new File("server.policy").getCanonicalPath());
			ActivationGroupDesc groupDesc = new ActivationGroupDesc(properties, null);
			ActivationGroupID groupId = ActivationGroup.getSystem().registerGroup(groupDesc); 
			String classPath = new File(".").getCanonicalFile().toURI().toString(); 
			ActivationDesc ad = new ActivationDesc(groupId, "Server.ShipImplementation", classPath, null);
			ShipInterface shipInterface = (ShipInterface)Activatable.register(ad);
			*/
			registryImplementation.rebind(name, shipInterface);
			System.out.println("[SERVER] Loaded, server can be used now. (" + name + ")");
						
			while(true) {
				Thread.sleep(500);
				shipInterface.updateStatus();
				Thread.sleep(500);
				shipInterface.updateStatus();
				Thread.sleep(500);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}